package com.finalProject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Jimmmy Smile
 * date: 2021-06-02 22:00::00
 * positioan: com.finalProject
 * e-mail:jimmysmileltl@163.com
 **/
public class DBManager {

    private DBHelper dbHelper;
    private String TBNAME;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }

    public void add(HousePrice item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("rank", item.getOrder());
        values.put("city", item.getCity());
        values.put("price", item.getPrice());
        values.put("rate", item.getRate());
        db.insert(TBNAME, null, values);
        db.close();
    }

    public void addAll(List<HousePrice> list){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (HousePrice item : list) {
            ContentValues values = new ContentValues();
            values.put("rank", item.getOrder());
            values.put("city", item.getCity());
            values.put("price", item.getPrice());
            values.put("rate", item.getRate());
            db.insert(TBNAME, null, values);

        }
        Log.i("addadd", "add成功 ");
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "ID=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void update(HousePrice item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("rank", item.getOrder());
        values.put("city", item.getCity());
        values.put("price", item.getPrice());
        values.put("rate", item.getRate());
        db.update(TBNAME, values, "city=?", new String[]{String.valueOf(item.getCity())});
        db.close();
    }

    public List<HousePrice> listAll(){
        List<HousePrice> rateList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            rateList = new ArrayList<HousePrice>();
            while(cursor.moveToNext()){

                HousePrice item = new HousePrice();
                item.setOrder(cursor.getString(cursor.getColumnIndex("rank")));
                item.setCity(cursor.getString(cursor.getColumnIndex("city")));
                item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
                item.setRate(cursor.getString(cursor.getColumnIndex("rate")));
                rateList.add(item);
            }
            Log.i("listlist", "listAll:list成功 ");
            cursor.close();
        }
        db.close();
        return rateList;

    }

    public HousePrice findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "ID=?", new String[]{String.valueOf(id)}, null, null, null);
        HousePrice HousePrice = null;
        if(cursor!=null && cursor.moveToFirst()){
            HousePrice = new HousePrice();
            HousePrice.setOrder(cursor.getString(cursor.getColumnIndex("rank")));
            HousePrice.setCity(cursor.getString(cursor.getColumnIndex("city")));
            HousePrice.setPrice(cursor.getString(cursor.getColumnIndex("price")));
            HousePrice.setRate(cursor.getString(cursor.getColumnIndex("rate")));
            cursor.close();
        }
        db.close();
        return HousePrice;
    }
}
